<script>
export default {
  name: "RoleAuthority",
  data() {
    return {
      Authorities: localStorage.getItem("authorities") ? JSON.parse(localStorage.getItem("authorities")) : {},
      tableData: [],
      roleID: undefined,
      roleCode: undefined,
      roleName: "",
      authorityID: undefined,
      authorityCode: undefined,
      authorityName: "",
      roleAuthority: {},
      dialogFormVisible: false,
      deleted_id: 0,
      ifSelecting: false,
      DeleteDisabled: false,
    }
  },
  created(){
    this.firstLoad();
    this.DeleteDisabled = this.Authorities.Authority22;
  },
  methods: {
    firstLoad() {
      this.request.get("http://localhost:9090/roleAuthority/all").then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
    },
    load() {
      this.request.get("http://localhost:9090/roleAuthority/roleID",  {
        params: {
          roleID: this.roleID
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
    },
    reset() {
      this.roleID = undefined;
      this.firstLoad();
    },
    save() {
      this.request.post("http://localhost:9090/roleAuthority/save", this.roleAuthority).then(res => {
        if (res) {
          this.$message.success("保存成功");
          this.dialogFormVisible = false;
          this.firstLoad();
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.roleAuthority = {};
    },
    handleDel(roleID, authorityID) {
      this.request.delete("http://localhost:9090/roleAuthority/" + roleID + "/" + authorityID).then(res => {
        if (res) {
          this.$message.success("删除成功");
          this.firstLoad();
        } else {
          this.$message.error("删除失败");
        }
      })
    }
  }
}
</script>

<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 150px" placeholder="请输入角色ID" suffix-icon="el-icon-message" class="ml-5" v-model="roleID" :disabled="!this.Authorities.Authority23"></el-input>
      <el-button class="ml-5" type="primary" @click="load" :disabled="!this.Authorities.Authority23">搜索</el-button>
      <el-button type="warning" @click="reset" :disabled="!this.Authorities.Authority23">重置</el-button>
      <el-button class=“ml-5” type="primary" @click="handleAdd" :disabled="!this.Authorities.Authority21">新增<i class="el-icon-circle-plus-outline"></i></el-button>
    </div>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg">
      <el-table-column prop="roleID" label="角色ID" width="80"></el-table-column>
      <el-table-column prop="roleCode" label="角色编码" width="140"></el-table-column>
      <el-table-column prop="roleName" label="角色名" width="120"></el-table-column>
      <el-table-column prop="authorityID" label="权限ID" width="120"></el-table-column>
      <el-table-column prop="authorityCode" label="权限编码" width="120"></el-table-column>
      <el-table-column prop="authorityName" label="权限名"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              title="确定删除这条数据吗？"
              icon="el-icon-info"
              icon-color="red"
              confirm-button-text="确定"
              cancel-button-text="取消"
              @confirm="handleDel(scope.row.roleID, scope.row.authorityID)"
          >
            <el-button type="danger" slot="reference"  :disabled="!DeleteDisabled">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="新增角色权限" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="100px">
        <el-form-item label="角色ID">
          <el-input v-model="roleAuthority.roleID" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限ID">
          <el-input v-model="roleAuthority.authorityID" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>

</style>